package com.cnap.multiclouldmgmt.util;

import com.cnap.multiclouldmgmt.model.RepByPage;

import java.util.List;
/**
 * @author dyq
 * 工具类
 */
public class OtherUtils {

    /**
     *
     * @param data
     * @return
     */
    public static int changeIntegerToInt(Integer data){
        if (data == null){
            return 0;
        }
        return data;
    }

    /**
     *
     * @param infoList
     * @param pageNo
     * @param pageSize
     * @param <T>
     * @return
     */
    public static <T> RepByPage<T> getResultFromList(List<T> infoList,
                                                     int pageNo, int pageSize){

        RepByPage<T> result = new RepByPage<>();
        result.setPageNo(pageNo);
        result.setPageSize(pageSize);
        result.setTotalNum(0);

        if (infoList != null){

            int size = infoList.size();
            result.setTotalNum(size);

            int beginIndex = (pageNo - 1)*pageSize;
            if (beginIndex < size) {

                int endIndex = Math.min((pageNo * pageSize), size);

                result.setResult(infoList.subList(beginIndex, endIndex));
            }
        }

        return result;
    }
}
